﻿using System;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DataAccess.Models;

namespace KPI.Controls
{
	public partial class TheGroupKPIs : System.Web.UI.UserControl
	{
		private int _ItemCount;

		public Group TheGroup
		{
			get;
			set;
		}

		public DataAccess.Models.LoginUserInfo TheCurrentLoginUser
		{
			get
			{
				return KPISiteContext.Current.CurrentUser;
			}
		}

		public string CurrentUserRole
		{
			get
			{
				var result = string.Empty;

				if(TheCurrentLoginUser != null)
				{
					result = TheCurrentLoginUser.RoleName;
				}

				return result;
			}
		}

		public int CurrentUserGroupID
		{
			get
			{
				var result = 0;

				if(TheCurrentLoginUser != null)
				{
					result = TheCurrentLoginUser.DeptID;
				}

				return result;
			}
		}

		public int TheYear
		{
			get;
			set;
		}

		public bool ShowOperator
		{
			get;
			set;
		}

		public int ShowEmptyRows
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if(TheGroup != null)
			{
				var kpiTable = BuildKPITable();

				if(kpiTable != null)
				{
					thegroupkpiscontainer.Controls.Add(kpiTable);

					BindKPIProgress();

					var showEndOfTheYearAssessment = ShowEndOfTheYearAssessment();
					thEndOfTheYearAssessment.Visible = showEndOfTheYearAssessment;
					thKPIProgressRemark.Visible = showEndOfTheYearAssessment;

					if(ShowOperator)
					{
						if(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID))
						{
							thKPIProgress_KPIContent.Style["width"] = "190px";
							thKPIProgress_Quarter1Progress.Style["width"] = "160px";
							thKPIProgress_Quarter2Progress.Style["width"] = "160px";
							thKPIProgress_Quarter3Progress.Style["width"] = "160px";
							thKPIProgress_Quarter4Progress.Style["width"] = "160px";
						}
					}
					else
					{
						if(CurrentUserRole.Equals("visitors") ||
							(CurrentUserRole.Equals("unitadministrators") && !CurrentUserGroupID.Equals(TheGroup.GroupID)))
						{
							thKPIProgress_KPIContent.Style["width"] = "190px";
							thKPIProgress_Quarter1Progress.Style["width"] = "160px";
							thKPIProgress_Quarter2Progress.Style["width"] = "160px";
							thKPIProgress_Quarter3Progress.Style["width"] = "160px";
							thKPIProgress_Quarter4Progress.Style["width"] = "160px";
						}
					}
				}
			}
		}

		protected void rptKPIProgress_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as DataAccess.Models.KPIProgress;

			if(data != null)
			{
				var groupCell = e.Item.FindControl("groupCell") as HtmlTableCell;
				var lRowNum = e.Item.FindControl("lRowNum") as Literal;
				var lKPIContent = e.Item.FindControl("lKPIContent") as Literal;
				var lQuarter1Progress = e.Item.FindControl("lQuarter1Progress") as Literal;
				var lQuarter2Progress = e.Item.FindControl("lQuarter2Progress") as Literal;
				var lQuarter3Progress = e.Item.FindControl("lQuarter3Progress") as Literal;
				var lQuarter4Progress = e.Item.FindControl("lQuarter4Progress") as Literal;
				var lEndOfTheYearAssessment = e.Item.FindControl("lEndOfTheYearAssessment") as Literal;
				var lKPIProgressRemark = e.Item.FindControl("lKPIProgressRemark") as Literal;
				var lKPIContent_Full = e.Item.FindControl("lKPIContent_Full") as Literal;
				var lQuarter1Progress_Full = e.Item.FindControl("lQuarter1Progress_Full") as Literal;
				var lQuarter2Progress_Full = e.Item.FindControl("lQuarter2Progress_Full") as Literal;
				var lQuarter3Progress_Full = e.Item.FindControl("lQuarter3Progress_Full") as Literal;
				var lQuarter4Progress_Full = e.Item.FindControl("lQuarter4Progress_Full") as Literal;
				var lEndOfTheYearAssessment_Full = e.Item.FindControl("lEndOfTheYearAssessment_Full") as Literal;
				var lKPIProgressRemark_Full = e.Item.FindControl("lKPIProgressRemark_Full") as Literal;

				var btnExpand_KPIContent = e.Item.FindControl("btnExpand_KPIContent") as Button;
				var btnExpand_Quarter1 = e.Item.FindControl("btnExpand_Quarter1") as Button;
				var btnExpand_Quarter2 = e.Item.FindControl("btnExpand_Quarter2") as Button;
				var btnExpand_Quarter3 = e.Item.FindControl("btnExpand_Quarter3") as Button;
				var btnExpand_Quarter4 = e.Item.FindControl("btnExpand_Quarter4") as Button;
				var btnExpand_EndOfTheYearAssessment = e.Item.FindControl("btnExpand_EndOfTheYearAssessment") as Button;
				var btnExpand_KPIProgressRemark = e.Item.FindControl("btnExpand_KPIProgressRemark") as Button;

				var imgKPIProgressState = e.Item.FindControl("imgKPIProgressState") as Image;

				var cell_kpicontent_progress = e.Item.FindControl("cell_kpicontent_progress") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_quarter1progress = e.Item.FindControl("cell_quarter1progress") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_quarter2progress = e.Item.FindControl("cell_quarter2progress") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_quarter3progress = e.Item.FindControl("cell_quarter3progress") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_quarter4progress = e.Item.FindControl("cell_quarter4progress") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_endoftheyearassessment = e.Item.FindControl("cell_endoftheyearassessment") as System.Web.UI.HtmlControls.HtmlTableCell;
				var cell_remark = e.Item.FindControl("cell_remark") as System.Web.UI.HtmlControls.HtmlTableCell;

				var tdEndOfTheYearAssessment = e.Item.FindControl("tdEndOfTheYearAssessment") as System.Web.UI.HtmlControls.HtmlTableCell;
				var tdKPIProgressRemark = e.Item.FindControl("tdKPIProgressRemark") as System.Web.UI.HtmlControls.HtmlTableCell;

				var showEndOfTheYearAssessment = ShowEndOfTheYearAssessment();
				tdEndOfTheYearAssessment.Visible = showEndOfTheYearAssessment;
				tdKPIProgressRemark.Visible = showEndOfTheYearAssessment;

				if(!showEndOfTheYearAssessment)
				{
					cell_quarter1progress.ColSpan = 7;
					cell_quarter2progress.ColSpan = 7;
					cell_quarter3progress.ColSpan = 7;
					cell_quarter4progress.ColSpan = 7;
					cell_kpicontent_progress.ColSpan = 7;
				}

				lRowNum.Text = (e.Item.ItemIndex + 1).ToString();
				if(e.Item.ItemIndex == 0)
				{
					var lGroup = e.Item.FindControl("lGroup") as Literal;
					lGroup.Text = TheGroup.GroupShortName;

					groupCell.RowSpan = _ItemCount * 8 + 1;
				}
				else
				{
					groupCell.Visible = false;
				}

				var kpiContent = (data.TheKPI.KPIContent.Length > 8 ? data.TheKPI.KPIContent.Substring(0, 8) : data.TheKPI.KPIContent);

				var tagAddLength = false;

				if(ShowOperator)
				{
					if(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID))
					{
						kpiContent = (data.TheKPI.KPIContent.Length > 14 ? data.TheKPI.KPIContent.Substring(0, 14) : data.TheKPI.KPIContent);
						tagAddLength = true;
					}
				}
				else
				{
					if(CurrentUserRole.Equals("visitors") ||
						(CurrentUserRole.Equals("unitadministrators") && !CurrentUserGroupID.Equals(TheGroup.GroupID)))
					{
						kpiContent = (data.TheKPI.KPIContent.Length > 14 ? data.TheKPI.KPIContent.Substring(0, 14) : data.TheKPI.KPIContent);
						tagAddLength = true;
					}
				}

				lKPIContent.Text = kpiContent;
				lKPIContent_Full.Text = data.TheKPI.KPIContent;
				btnExpand_KPIContent.OnClientClick = "return btnExpand_click(this, '#" + cell_kpicontent_progress.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";

				if(!string.IsNullOrWhiteSpace(data.Quarter1Progress))
				{
					var q1p = CommonHelper.HtmlStripTags(data.Quarter1Progress).Trim();
					if(tagAddLength)
					{
						lQuarter1Progress.Text = (q1p.Length > 11 ? q1p.Substring(0, 11) : q1p);
					}
					else
					{
						lQuarter1Progress.Text = (q1p.Length > 8 ? q1p.Substring(0, 8) : q1p);
					}
					lQuarter1Progress_Full.Text = data.Quarter1Progress;
					btnExpand_Quarter1.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter1progress.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter1.Visible = false;
					cell_quarter1progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(data.Quarter2Progress))
				{
					var q2p = CommonHelper.HtmlStripTags(data.Quarter2Progress).Trim();
					if(tagAddLength)
					{
						lQuarter2Progress.Text = (q2p.Length > 11 ? q2p.Substring(0, 11) : q2p);
					}
					else
					{
						lQuarter2Progress.Text = (q2p.Length > 8 ? q2p.Substring(0, 8) : q2p);
					}

					lQuarter2Progress_Full.Text = data.Quarter2Progress;
					btnExpand_Quarter2.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter2progress.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter2.Visible = false;
					cell_quarter2progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(data.Quarter3Progress))
				{
					var q3p = CommonHelper.HtmlStripTags(data.Quarter3Progress).Trim();
					if(tagAddLength)
					{
						lQuarter3Progress.Text = (q3p.Length > 11 ? q3p.Substring(0, 11) : q3p);
					}
					else
					{
						lQuarter3Progress.Text = (q3p.Length > 8 ? q3p.Substring(0, 8) : q3p);
					}
					lQuarter3Progress_Full.Text = data.Quarter3Progress;
					btnExpand_Quarter3.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter3progress.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter3.Visible = false;
					cell_quarter3progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(data.Quarter4Progress))
				{
					var q4p = CommonHelper.HtmlStripTags(data.Quarter4Progress).Trim();
					if(tagAddLength)
					{
						lQuarter4Progress.Text = (q4p.Length > 11 ? q4p.Substring(0, 11) : q4p);
					}
					else
					{
						lQuarter4Progress.Text = (q4p.Length > 8 ? q4p.Substring(0, 8) : q4p);
					}
					lQuarter4Progress_Full.Text = data.Quarter4Progress;
					btnExpand_Quarter4.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter4progress.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter4.Visible = false;
					cell_quarter4progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(data.EndOfTheYearAssessment))
				{
					var eya = CommonHelper.HtmlStripTags(data.EndOfTheYearAssessment).Trim();
					lEndOfTheYearAssessment.Text = (eya.Length > 8 ? eya.Substring(0, 8) : eya);
					lEndOfTheYearAssessment_Full.Text = data.EndOfTheYearAssessment;
					btnExpand_EndOfTheYearAssessment.OnClientClick = "return btnExpand_click(this, '#" + cell_endoftheyearassessment.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_EndOfTheYearAssessment.Visible = false;
					cell_endoftheyearassessment.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(data.KPIProgressRemark))
				{
					lKPIProgressRemark.Text = (data.KPIProgressRemark.Length > 6 ? data.KPIProgressRemark.Substring(0, 6) : data.KPIProgressRemark);
					lKPIProgressRemark_Full.Text = data.KPIProgressRemark;
					btnExpand_KPIProgressRemark.OnClientClick = "return btnExpand_click(this, '#" + cell_remark.ClientID.ToString() + "', '#" + kpisprogress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_KPIProgressRemark.Visible = false;
					cell_remark.InnerHtml = string.Empty;
				}

				if(string.IsNullOrWhiteSpace(data.KPIProgressState))
				{
					imgKPIProgressState.Visible = false;
				}
				else
				{
					imgKPIProgressState.ImageUrl = "~/images/status_" + data.KPIProgressState + ".png";
					imgKPIProgressState.ToolTip = (data.KPIProgressState.Equals(DataAccess.Models.KPIProgressStates.Processing) ? "进行中" : (data.KPIProgressState.Equals(DataAccess.Models.KPIProgressStates.Complete) ? "完成" : "未完成"));
					imgKPIProgressState.AlternateText = imgKPIProgressState.ToolTip;
					imgKPIProgressState.CssClass = "kpiprogressstate";
				}
			}
		}

		private HtmlTable BuildKPITable()
		{
			HtmlTable result = null;

			if(TheGroup != null)
			{
				if(TheGroup.ListKPIs != null && TheGroup.ListKPIs.Count > 0)
				{
					result = new HtmlTable();
					result.CellPadding = 2;
					result.CellSpacing = 0;
					result.Border = 0;
					result.Attributes.Add("class", "listdata");

					System.Collections.Generic.IEnumerable<DataAccess.Models.KPI> kpis = null;

					if(TheYear > 0)
					{
						kpis = TheGroup.ListKPIs.Where(kpi => kpi.CurrentState != DataAccess.Models.KPIStates.Delete && kpi.TheYear == TheYear);
					}
					//else
					//{
					//  kpis = TheGroup.ListKPIs.Where(kpi => kpi.CurrentState != DataAccess.Models.KPIStates.Delete);
					//}

					if(kpis != null && kpis.Count() > 0)
					{
						result.Rows.Add(BuildKPITableHeaderRow());

						var lstKPIs = kpis.ToList();

						var extRows = 0;

						for(var i = 0; i < lstKPIs.Count(); i++)
						{
							var kpicontentcellID = string.Empty;
							var kpiContentRow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].KPIContent, "kpicontent", out kpicontentcellID);
							var kpiactionplancontentcellID = string.Empty;
							var kpiactionplancontentrow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].ActionPlans, "kpiactionplancontent", out kpiactionplancontentcellID);

							var responsedeptcellID = string.Empty;
							HtmlTableRow responsedeptrow = null;
							if(lstKPIs[i].ResponseDepartment.Contains(",") || lstKPIs[i].ResponseDepartment.Contains("，") || lstKPIs[i].ResponseDepartment.Contains("；") || lstKPIs[i].ResponseDepartment.Contains("、") || lstKPIs[i].ResponseDepartment.Contains(";") || lstKPIs[i].ResponseDepartment.Length > 4)
							{
								responsedeptrow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].ResponseDepartment, "responcedepartmentcontent", out responsedeptcellID);
								extRows++;
							}
							var responsablepersoncellID = string.Empty;
							HtmlTableRow responsablepersonrow = null;
							if(lstKPIs[i].ResponsablePerson.Contains(",") || lstKPIs[i].ResponsablePerson.Contains("，") || lstKPIs[i].ResponsablePerson.Contains("；") || lstKPIs[i].ResponsablePerson.Contains("、") || lstKPIs[i].ResponsablePerson.Contains(";"))
							{
								responsablepersonrow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].ResponsablePerson, "responsablepersoncontent", out responsablepersoncellID);
								extRows++;
							}
							var responsibleleadershipcellID = string.Empty;
							HtmlTableRow responsibleleadershiprow = null;
							if(lstKPIs[i].ResponsibleLeadership.Contains(",") || lstKPIs[i].ResponsibleLeadership.Contains("，") || lstKPIs[i].ResponsibleLeadership.Contains("；") || lstKPIs[i].ResponsibleLeadership.Contains("、") || lstKPIs[i].ResponsibleLeadership.Contains(";"))
							{
								responsibleleadershiprow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].ResponsibleLeadership, "responsibleleadershipcontent", out responsibleleadershipcellID);
								extRows++;
							}
							var rltelcellID = string.Empty;
							HtmlTableRow rltelrow = null;
							var rptelcellID = string.Empty;
							HtmlTableRow rptelrow = null;
							if(CurrentUserRole.Equals("administrators") || CurrentUserGroupID.Equals(TheGroup.GroupID))
							{
								if(lstKPIs[i].RLTel.Contains(",") || lstKPIs[i].RLTel.Contains("，") || lstKPIs[i].RLTel.Contains("；") || lstKPIs[i].RLTel.Contains("、") || lstKPIs[i].RLTel.Contains(";"))
								{
									rltelrow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].RLTel, "rltelcontent", out rltelcellID);
									extRows++;
								}
								if(lstKPIs[i].RPTel.Contains(",") || lstKPIs[i].RPTel.Contains("，") || lstKPIs[i].RPTel.Contains("；") || lstKPIs[i].RPTel.Contains("、") || lstKPIs[i].RPTel.Contains(";"))
								{
									rptelrow = BuildKPITableItemContentRow(i + 1, lstKPIs[i].RPTel, "rptelcontent", out rptelcellID);
									extRows++;
								}
							}

							var kpiItem = BuildKPITableItemRow(lstKPIs[i], i + 1, lstKPIs.Count, kpicontentcellID, kpiactionplancontentcellID, responsedeptcellID, responsablepersoncellID, responsibleleadershipcellID, rltelcellID, rptelcellID);

							if(kpiItem != null)
							{
								result.Rows.Add(kpiItem);

								result.Rows.Add(kpiContentRow);
								result.Rows.Add(kpiactionplancontentrow);
								if(responsedeptrow != null)
								{
									result.Rows.Add(responsedeptrow);
								}
								if(responsablepersonrow != null)
								{
									result.Rows.Add(responsablepersonrow);
								}
								if(rptelrow != null)
								{
									result.Rows.Add(rptelrow);
								}
								if(responsibleleadershiprow != null)
								{
									result.Rows.Add(responsibleleadershiprow);
								}
								if(rltelrow != null)
								{
									result.Rows.Add(rltelrow);
								}
							}
						}

						if(extRows > 0)
						{
							result.Rows[1].Cells[0].RowSpan += extRows;
						}

						result.Rows.Add(BuildKPITableFooterRow());
					}
				}
				else if(ShowEmptyRows > 0)
				{
					result = new HtmlTable();
					result.CellPadding = 2;
					result.CellSpacing = 0;
					result.Border = 0;
					result.Attributes.Add("class", "listdata");

					result.Rows.Add(BuildKPITableHeaderRow());

					for(var i = 1; i <= ShowEmptyRows; i++)
					{
						result.Rows.Add(BuildKPITableEmptyRow(i));
					}

					result.Rows.Add(BuildKPITableFooterRow());
				}

			}

			return result;
		}

		/// <summary>
		/// 创建KPI表头
		/// </summary>
		/// <returns></returns>
		private HtmlTableRow BuildKPITableHeaderRow()
		{
			HtmlTableRow headRow = new HtmlTableRow();

			HtmlTableCell hCell1 = new HtmlTableCell("th");
			HtmlTableCell hCell2 = new HtmlTableCell("th");
			HtmlTableCell hCell3 = new HtmlTableCell("th");
			HtmlTableCell hCell4 = new HtmlTableCell("th");
			HtmlTableCell hCell5 = new HtmlTableCell("th");
			HtmlTableCell hCell6 = new HtmlTableCell("th");
			HtmlTableCell hCell7 = new HtmlTableCell("th");
			HtmlTableCell hCell8 = new HtmlTableCell("th");
			HtmlTableCell hCell9 = new HtmlTableCell("th");
			HtmlTableCell hCell10 = new HtmlTableCell("th");
			HtmlTableCell hCell11 = new HtmlTableCell("th");
			HtmlTableCell hCell12 = new HtmlTableCell("th");

			hCell1.InnerText = "单位";
			hCell1.Style.Add("width", "60px");
			hCell2.InnerText = "序号";
			hCell2.Style.Add("width", "28px");
			hCell3.InnerText = "指标内容";
			hCell3.Style.Add("width", "117px");
			hCell4.InnerText = "措施";
			hCell4.Style.Add("width", "115px");
			hCell5.InnerText = "开始时间";
			hCell5.Style.Add("width", "60px");
			hCell6.InnerText = "完成时间";
			hCell6.Style.Add("width", "60px");
			hCell7.InnerText = "责任科室";
			hCell7.Style.Add("width", "90px");
			hCell8.InnerText = "项目负责人";
			hCell8.Style.Add("width", "68px");
			hCell9.InnerText = "联系电话";
			hCell9.Style.Add("width", "75px");
			hCell10.InnerText = "分管领导";
			hCell10.Style.Add("width", "58px");
			hCell11.InnerText = "联系电话";
			hCell11.Style.Add("width", "75px");
			hCell12.InnerText = "操作";
			hCell12.Style.Add("width", "100px");

			headRow.Cells.Add(hCell1);
			headRow.Cells.Add(hCell2);
			headRow.Cells.Add(hCell3);
			headRow.Cells.Add(hCell4);
			headRow.Cells.Add(hCell5);
			headRow.Cells.Add(hCell6);
			headRow.Cells.Add(hCell7);
			headRow.Cells.Add(hCell8);
			if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				headRow.Cells.Add(hCell9);
			}
			headRow.Cells.Add(hCell10);
			if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				headRow.Cells.Add(hCell11);
			}
			if(ShowOperator && (CurrentUserRole.Equals("administrators") || CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				headRow.Cells.Add(hCell12);
			}

			if(ShowOperator)
			{
				if(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID))
				{
					hCell3.Style["width"] = "190px";
					hCell4.Style["width"] = "200px";
					hCell5.Style["width"] = "80px";
					hCell6.Style["width"] = "80px";
					hCell7.Style["width"] = "120px";
					hCell8.Style["width"] = "90px";
					hCell10.Style["width"] = "80px";
				}
			}
			else
			{
				if(CurrentUserRole.Equals("visitors") ||
					(CurrentUserRole.Equals("unitadministrators") && !CurrentUserGroupID.Equals(TheGroup.GroupID)))
				{
					hCell3.Style["width"] = "190px";
					hCell4.Style["width"] = "214px";
					hCell5.Style["width"] = "90px";
					hCell6.Style["width"] = "90px";
					hCell7.Style["width"] = "130px";
					hCell8.Style["width"] = "100px";
					hCell10.Style["width"] = "90px";
				}

				if(CurrentUserRole.Equals("administrators") ||
						(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
				{
					hCell4.Style["width"] = "214px";
					//hCell5.Style["width"] = "90px";
					//hCell6.Style["width"] = "90px";
					//hCell7.Style["width"] = "130px";
					//hCell8.Style["width"] = "100px";
					//hCell10.Style["width"] = "90px";
				}

				if(CurrentUserRole.Equals("supervisitors"))
				{
					hCell4.Style["width"] = "214px";
					hCell5.Style["width"] = "90px";
					hCell6.Style["width"] = "90px";
					hCell7.Style["width"] = "130px";
					hCell8.Style["width"] = "100px";
					hCell10.Style["width"] = "90px";
				}
			}

			return headRow;
		}

		private HtmlTableRow BuildKPITableFooterRow()
		{
			HtmlTableRow footerRow = new HtmlTableRow();

			footerRow.Style.Add("height", "0px");

			return footerRow;
		}

		/// <summary>
		/// 创建KPI表数据行
		/// </summary>
		/// <param name="kpi"></param>
		/// <param name="rowIndex"></param>
		/// <param name="rows"></param>
		/// <returns></returns>
		private HtmlTableRow BuildKPITableItemRow(DataAccess.Models.KPI kpi, int rowIndex, int rows, string kpicontentcellID, string kpiactionplancontentcellID, string responsedeptcellID, string responsablepersoncellID, string responsibleleadershipcellID, string rltelcellID, string rptelcellID)
		{
			HtmlTableRow result = null;

			if(kpi != null)
			{
				result = new HtmlTableRow();
				//result.Attributes.Add("class", "rowitem");

				if(rowIndex == 1)
				{
					HtmlTableCell cell1 = new HtmlTableCell("td");
					cell1.RowSpan = rows * 3 + 1;

					HtmlGenericControl divGroupNameContainer = new HtmlGenericControl("div");
					divGroupNameContainer.Attributes.Add("class", "groupnamecontainer");
					divGroupNameContainer.InnerText = TheGroup.GroupShortName;
					//cell1.InnerText = TheGroup.GroupDispName;

					//var br = new HtmlGenericControl("br");

					var btnExpandKPIProgress = new HtmlAnchor();
					btnExpandKPIProgress.InnerText = "显示进度";
					btnExpandKPIProgress.Title = "展开/折叠进度";
					btnExpandKPIProgress.HRef = "javascript:;";
					btnExpandKPIProgress.Attributes.Add("onclick", "return btnExpandKPIProgress_Click(this, '#" + kpisprogress.ClientID.ToString() + "');");
					btnExpandKPIProgress.Attributes.Add("class", "linkbutton");

					//cell1.Controls.Add(br);
					cell1.Style.Add("text-align", "center");
					cell1.Controls.Add(divGroupNameContainer);
					cell1.Controls.Add(btnExpandKPIProgress);
					result.Cells.Add(cell1);
				}

				HtmlTableCell cell2 = new HtmlTableCell("td");
				HtmlTableCell cell3 = new HtmlTableCell("td");
				HtmlTableCell cell4 = new HtmlTableCell("td");
				HtmlTableCell cell5 = new HtmlTableCell("td");
				HtmlTableCell cell6 = new HtmlTableCell("td");
				HtmlTableCell cell7 = new HtmlTableCell("td");
				HtmlTableCell cell8 = new HtmlTableCell("td");
				HtmlTableCell cell9 = new HtmlTableCell("td");
				HtmlTableCell cell10 = new HtmlTableCell("td");
				HtmlTableCell cell11 = new HtmlTableCell("td");
				HtmlTableCell cell12 = new HtmlTableCell("td");

				cell2.Style.Add("text-align", "center");
				//cell7.Style.Add("text-align", "center");
				//cell8.Style.Add("text-align", "center");
				//cell9.Style.Add("text-align", "center");
				//cell10.Style.Add("text-align", "center");
				//cell11.Style.Add("text-align", "center");
				cell2.InnerText = rowIndex.ToString();
				cell5.InnerText = kpi.StartTime.HasValue ? kpi.StartTime.Value.ToShortDateString() : string.Empty;
				cell6.InnerText = kpi.FinishTime.HasValue ? kpi.FinishTime.Value.ToShortDateString() : string.Empty;
				//cell7.InnerText = kpi.ResponseDepartment;
				//cell8.InnerText = kpi.ResponsablePerson;
				//cell9.InnerText = kpi.RPTel;
				//cell10.InnerText = kpi.ResponsibleLeadership;
				//cell11.InnerText = kpi.RLTel;

				HtmlGenericControl spanKPIContent = new HtmlGenericControl("span");

				//HtmlGenericControl labelKPIContent = new HtmlGenericControl("label");
				var displayKPIContent = kpi.KPIContent.Length > 8 ? kpi.KPIContent.Substring(0, 8) : kpi.KPIContent;
				if(ShowOperator)
				{
					if(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID))
					{
						displayKPIContent = kpi.KPIContent.Length > 14 ? kpi.KPIContent.Substring(0, 14) : kpi.KPIContent;
					}
				}
				else
				{
					if(CurrentUserRole.Equals("visitors") ||
						(CurrentUserRole.Equals("unitadministrators") && !CurrentUserGroupID.Equals(TheGroup.GroupID)))
					{
						displayKPIContent = kpi.KPIContent.Length > 14 ? kpi.KPIContent.Substring(0, 14) : kpi.KPIContent;
					}
				}

				spanKPIContent.InnerText = displayKPIContent;
				//if(!ShowOperator && !CurrentUserRole.Equals("administrators") || (ShowOperator && CurrentUserGroupID.Equals(TheGroup.GroupID)))
				//{
				//  spanKPIContent.InnerText = kpi.KPIContent.Length > 14 ? kpi.KPIContent.Substring(0, 14) : kpi.KPIContent;
				//}
				//else
				//{
				//  spanKPIContent.InnerText = kpi.KPIContent.Length > 8 ? kpi.KPIContent.Substring(0, 8) : kpi.KPIContent;
				//}

				HtmlInputButton btnExpandKPIContent = new HtmlInputButton("button");
				btnExpandKPIContent.Attributes.Add("class", "expand kpicontent");
				btnExpandKPIContent.Attributes.Add("title", "点击详情");
				btnExpandKPIContent.Attributes.Add("onclick", "btnExpand_click(this, '#" + kpicontentcellID + "');");

				//spanKPIContent.Controls.Add(labelKPIContent);
				spanKPIContent.Controls.Add(btnExpandKPIContent);

				cell3.Style.Add("width", "120px");
				cell3.Controls.Add(spanKPIContent);
				//cell3.Controls.Add(btnExpandKPIContent);

				if(!string.IsNullOrWhiteSpace(kpi.ActionPlans))
				{
					HtmlGenericControl spanKPIActionPlan = new HtmlGenericControl("span");

					var displayKPIActionPlan = kpi.ActionPlans.Length > 8 ? kpi.ActionPlans.Substring(0, 8) : kpi.ActionPlans;
					if(ShowOperator)
					{
						if(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID))
						{
							displayKPIActionPlan = kpi.ActionPlans.Length > 13 ? kpi.ActionPlans.Substring(0, 13) : kpi.ActionPlans;
						}
					}
					else
					{
						if(CurrentUserRole.Equals("visitors") ||
							(CurrentUserRole.Equals("unitadministrators") && !CurrentUserGroupID.Equals(TheGroup.GroupID)))
						{
							displayKPIActionPlan = kpi.ActionPlans.Length > 16 ? kpi.ActionPlans.Substring(0, 16) : kpi.ActionPlans;
						}

						if(CurrentUserRole.Equals("administrators") ||
								(CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
						{
							displayKPIActionPlan = kpi.ActionPlans.Length > 16 ? kpi.ActionPlans.Substring(0, 16) : kpi.ActionPlans;
						}

						if(CurrentUserRole.Equals("supervisitors"))
						{
							displayKPIActionPlan = kpi.ActionPlans.Length > 16 ? kpi.ActionPlans.Substring(0, 16) : kpi.ActionPlans;
						}
					}

					spanKPIActionPlan.InnerText = displayKPIActionPlan;

					HtmlInputButton btnExpandActionPlan = new HtmlInputButton("button");
					btnExpandActionPlan.Attributes.Add("class", "expand kpiactionplancontent");
					btnExpandActionPlan.Attributes.Add("title", "点击详情");
					btnExpandActionPlan.Attributes.Add("onclick", "btnExpand_click(this, '#" + kpiactionplancontentcellID + "');");

					spanKPIActionPlan.Controls.Add(btnExpandActionPlan);

					cell4.Controls.Add(spanKPIActionPlan);
				}

				if(!string.IsNullOrWhiteSpace(responsedeptcellID))
				{
					HtmlGenericControl spanresponsedept = new HtmlGenericControl("span");
					spanresponsedept.InnerText = kpi.ResponseDepartment.Substring(0, 4);

					HtmlInputButton btnresponsedept = new HtmlInputButton("button");
					btnresponsedept.Attributes.Add("class", "expand responcedepartmentcontent");
					btnresponsedept.Attributes.Add("title", "更多");
					btnresponsedept.Attributes.Add("onclick", "btnExpand_click(this, '#" + responsedeptcellID + "');");

					spanresponsedept.Controls.Add(btnresponsedept);

					cell7.Controls.Add(spanresponsedept);
				}
				else
				{
					cell7.InnerText = kpi.ResponseDepartment;
				}

				if(!string.IsNullOrWhiteSpace(responsablepersoncellID))
				{
					HtmlGenericControl spanresponsableperson = new HtmlGenericControl("span");
					spanresponsableperson.InnerText = kpi.ResponsablePerson.Substring(0, 3);

					HtmlInputButton btnresponsableperson = new HtmlInputButton("button");
					btnresponsableperson.Attributes.Add("class", "expand responsablepersoncontent");
					btnresponsableperson.Attributes.Add("title", "更多");
					btnresponsableperson.Attributes.Add("onclick", "btnExpand_click(this, '#" + responsablepersoncellID + "');");

					spanresponsableperson.Controls.Add(btnresponsableperson);

					cell8.Controls.Add(spanresponsableperson);
				}
				else
				{
					cell8.InnerText = kpi.ResponsablePerson;
				}

				if(!string.IsNullOrWhiteSpace(rptelcellID))
				{
					HtmlGenericControl spanrptel = new HtmlGenericControl("span");
					spanrptel.InnerText = kpi.RPTel.Substring(0, 8);

					HtmlInputButton btnrptel = new HtmlInputButton("button");
					btnrptel.Attributes.Add("class", "expand rptelcontent");
					btnrptel.Attributes.Add("title", "更多");
					btnrptel.Attributes.Add("onclick", "btnExpand_click(this, '#" + rptelcellID + "');");

					spanrptel.Controls.Add(btnrptel);

					cell9.Controls.Add(spanrptel);
				}
				else
				{
					cell9.InnerText = kpi.RPTel;
				}

				if(!string.IsNullOrWhiteSpace(responsibleleadershipcellID))
				{
					HtmlGenericControl spanresponsibleleadership = new HtmlGenericControl("span");
					spanresponsibleleadership.InnerText = kpi.ResponsibleLeadership.Substring(0, 3);

					HtmlInputButton btnresponsibleleadership = new HtmlInputButton("button");
					btnresponsibleleadership.Attributes.Add("class", "expand responsibleleadershipcontent");
					btnresponsibleleadership.Attributes.Add("title", "更多");
					btnresponsibleleadership.Attributes.Add("onclick", "btnExpand_click(this, '#" + responsibleleadershipcellID + "');");

					spanresponsibleleadership.Controls.Add(btnresponsibleleadership);

					cell10.Controls.Add(spanresponsibleleadership);
				}
				else
				{
					cell10.InnerText = kpi.ResponsibleLeadership;
				}

				if(!string.IsNullOrWhiteSpace(rltelcellID))
				{
					HtmlGenericControl spanrltel = new HtmlGenericControl("span");
					spanrltel.InnerText = kpi.RLTel.Substring(0, 8);

					HtmlInputButton btnrltel = new HtmlInputButton("button");
					btnrltel.Attributes.Add("class", "expand rltelcontent");
					btnrltel.Attributes.Add("title", "更多");
					btnrltel.Attributes.Add("onclick", "btnExpand_click(this, '#" + rltelcellID + "');");

					spanrltel.Controls.Add(btnrltel);

					cell11.Controls.Add(spanrltel);
				}
				else
				{
					cell11.InnerText = kpi.RLTel;
				}

				result.Cells.Add(cell2);
				result.Cells.Add(cell3);
				result.Cells.Add(cell4);
				result.Cells.Add(cell5);
				result.Cells.Add(cell6);
				result.Cells.Add(cell7);
				result.Cells.Add(cell8);
				if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
				{
					result.Cells.Add(cell9);
				}
				result.Cells.Add(cell10);
				if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
				{
					result.Cells.Add(cell11);
				}
				if(ShowOperator && (CurrentUserRole.Equals("administrators") || CurrentUserGroupID.Equals(TheGroup.GroupID)))
				{
					var operators = BuildKPIOperator(kpi);

					if(operators.Items.Count > 1)
					{
						cell12.Controls.Add(operators);
					}

					result.Cells.Add(cell12);
				}
			}

			return result;
		}

		private HtmlTableRow BuildKPITableEmptyRow(int rowIndex)
		{
			HtmlTableRow result = null;

			result = new HtmlTableRow();
			//result.Attributes.Add("class", "rowitem");

			if(rowIndex == 1)
			{
				HtmlTableCell cell1 = new HtmlTableCell("td");
				cell1.RowSpan = ShowEmptyRows + 1;

				HtmlGenericControl divGroupNameContainer = new HtmlGenericControl("div");
				divGroupNameContainer.Attributes.Add("class", "groupnamecontainer");
				divGroupNameContainer.InnerText = TheGroup.GroupShortName;

				cell1.Style.Add("text-align", "center");
				cell1.Controls.Add(divGroupNameContainer);
				//cell1.Controls.Add(btnExpandKPIProgress);
				result.Cells.Add(cell1);
			}

			HtmlTableCell cell2 = new HtmlTableCell("td");
			HtmlTableCell cell3 = new HtmlTableCell("td");
			HtmlTableCell cell4 = new HtmlTableCell("td");
			HtmlTableCell cell5 = new HtmlTableCell("td");
			HtmlTableCell cell6 = new HtmlTableCell("td");
			HtmlTableCell cell7 = new HtmlTableCell("td");
			HtmlTableCell cell8 = new HtmlTableCell("td");
			HtmlTableCell cell9 = new HtmlTableCell("td");
			HtmlTableCell cell10 = new HtmlTableCell("td");
			HtmlTableCell cell11 = new HtmlTableCell("td");
			HtmlTableCell cell12 = new HtmlTableCell("td");

			cell2.InnerText = rowIndex.ToString();
			cell2.Style.Add("text-align", "center");
			cell3.Style.Add("width", "120px");

			result.Cells.Add(cell2);
			result.Cells.Add(cell3);
			result.Cells.Add(cell4);
			result.Cells.Add(cell5);
			result.Cells.Add(cell6);
			result.Cells.Add(cell7);
			result.Cells.Add(cell8);
			if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				result.Cells.Add(cell9);
			}
			result.Cells.Add(cell10);
			if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				result.Cells.Add(cell11);
			}
			if(ShowOperator && (CurrentUserRole.Equals("administrators") || CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				result.Cells.Add(cell12);
			}

			return result;
		}

		/// <summary>
		/// 创建KPI内容详情行
		/// </summary>
		/// <param name="index"></param>
		/// <param name="content"></param>
		/// <param name="rowClass"></param>
		/// <param name="contentCellID"></param>
		/// <returns></returns>
		private HtmlTableRow BuildKPITableItemContentRow(int index, string content, string rowClass, out string contentCellID)
		{
			HtmlTableRow result = new HtmlTableRow();
			contentCellID = string.Empty;

			result.Style.Add("display", "none");
			result.Attributes.Add("class", rowClass);

			HtmlTableCell cell = new HtmlTableCell("td");
			cell.ClientIDMode = System.Web.UI.ClientIDMode.Static;
			cell.ID = rowClass + "_" + TheGroup.GroupID.ToString() + "_" + index.ToString();
			if(CurrentUserRole.Equals("administrators") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID.Equals(TheGroup.GroupID)))
			{
				if(ShowOperator)
				{
					cell.ColSpan = 11;
				}
				else
				{
					cell.ColSpan = 10;
				}
			}
			else
			{
				cell.ColSpan = 8;
			}

			HtmlGenericControl divContentBox = new HtmlGenericControl("div");
			divContentBox.InnerHtml = content ?? string.Empty;
			divContentBox.Attributes.Add("class", "contentbox");

			contentCellID = cell.ID;

			cell.Controls.Add(divContentBox);
			result.Controls.Add(cell);

			return result;
		}

		private System.Web.UI.HtmlControls.HtmlSelect BuildKPIOperator(DataAccess.Models.KPI kpi)
		{
			System.Web.UI.HtmlControls.HtmlSelect result = new HtmlSelect();
			result.Items.Add(new System.Web.UI.WebControls.ListItem("选择操作"));
			result.ID = "listKPIOperator";
			result.Attributes.Add("onchange", "listKPIOperator_Change(this, " + kpi.KPIID.ToString() + ");");

			if(kpi != null && !string.IsNullOrWhiteSpace(kpi.CurrentState))
			{
				switch(kpi.CurrentState)
				{
					case DataAccess.Models.KPIStates.Review_Pending:
						{
							if(CurrentUserRole == "administrators")
							{
								var reviewApprovedItem = new System.Web.UI.WebControls.ListItem("审核通过");
								var reviewNotApprovedItem = new System.Web.UI.WebControls.ListItem("审核不通过");

								result.Items.Add(reviewApprovedItem);
								result.Items.Add(reviewNotApprovedItem);
							}
							else if(CurrentUserRole == "unitadministrators")
							{
								var editItem = new System.Web.UI.WebControls.ListItem("修改");
								var deleteItem = new System.Web.UI.WebControls.ListItem("删除");

								result.Items.Add(editItem);
								result.Items.Add(deleteItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.Review_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var editItem = new System.Web.UI.WebControls.ListItem("申请修改");
									result.Items.Add(editItem);

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Review_Not_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								var editItem = new System.Web.UI.WebControls.ListItem("修改");
								var deleteItem = new System.Web.UI.WebControls.ListItem("删除");

								result.Items.Add(editItem);
								result.Items.Add(deleteItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Applying:
						{
							if(CurrentUserRole == "administrators")
							{
								var kpiModificationRequestReviewApprovedItem = new System.Web.UI.WebControls.ListItem("允许修改");
								var kpiModificationRequestReviewNotApprovedItem = new System.Web.UI.WebControls.ListItem("不允许修改");

								result.Items.Add(kpiModificationRequestReviewApprovedItem);
								result.Items.Add(kpiModificationRequestReviewNotApprovedItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								var editItem = new System.Web.UI.WebControls.ListItem("修改");
								var deleteItem = new System.Web.UI.WebControls.ListItem("删除");

								result.Items.Add(editItem);
								result.Items.Add(deleteItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Not_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var editItem = new System.Web.UI.WebControls.ListItem("申请修改");
									result.Items.Add(editItem);

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Pending:
						{
							if(CurrentUserRole == "administrators")
							{
								var kpiModificationModifiedReviewPendingItem = new System.Web.UI.WebControls.ListItem("审核修改的KPI");

								result.Items.Add(kpiModificationModifiedReviewPendingItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Not_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								var editItem = new System.Web.UI.WebControls.ListItem("修改");
								var deleteItem = new System.Web.UI.WebControls.ListItem("删除");

								result.Items.Add(editItem);
								result.Items.Add(deleteItem);

								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var editItem = new System.Web.UI.WebControls.ListItem("申请修改");
									result.Items.Add(editItem);

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Reporting:
						{
							if(CurrentUserRole == "administrators")
							{
								//var progressViewItem = new System.Web.UI.WebControls.ListItem("查看进度");

								//result.Items.Add(progressViewItem);
							}
							else if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									//var editItem = new System.Web.UI.WebControls.ListItem("申请修改");
									//result.Items.Add(editItem);
									
									if(CanProgressModificationRequest(kpi))
									{
										var progressModificationRequestItem = new System.Web.UI.WebControls.ListItem("申请修改进度");
										result.Items.Add(progressModificationRequestItem);
									}

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Applying:
						{
							if(CurrentUserRole == "administrators")
							{
								var progressModificationRequestReviewApprovedItem = new System.Web.UI.WebControls.ListItem("允许修改进度");
								var progressModificationRequestReviewNotApprovedItem = new System.Web.UI.WebControls.ListItem("不允许修改进度");

								result.Items.Add(progressModificationRequestReviewApprovedItem);
								result.Items.Add(progressModificationRequestReviewNotApprovedItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Not_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									if(CanProgressModificationRequest(kpi))
									{
										var progressModificationRequestItem = new System.Web.UI.WebControls.ListItem("申请修改进度");
										result.Items.Add(progressModificationRequestItem);
									}

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var progressModificationItem = new System.Web.UI.WebControls.ListItem("修改进度");

									result.Items.Add(progressModificationItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Pending:
						{
							if(CurrentUserRole == "administrators")
							{
								var progressModificationModifiedReview = new System.Web.UI.WebControls.ListItem("审核修改的进度");

								result.Items.Add(progressModificationModifiedReview);
							}
							else if(CurrentUserRole == "unitadministrators")
							{
								var progressModificationItem = new System.Web.UI.WebControls.ListItem("修改进度");

								result.Items.Add(progressModificationItem);
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Not_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									var progressModificationItem = new System.Web.UI.WebControls.ListItem("修改进度");
									result.Items.Add(progressModificationItem);

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Approved:
						{
							if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									if(CanProgressModificationRequest(kpi))
									{
										var progressModificationRequestItem = new System.Web.UI.WebControls.ListItem("申请修改进度");
										result.Items.Add(progressModificationRequestItem);
									}

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Progress_Report_End:
						{
							if(CurrentUserRole == "administrators")
							{
								if(kpi.FinishTime < DateTime.Now)
								{
									var endOfTheYearAssessmentItem = new System.Web.UI.WebControls.ListItem("年底考核");

									result.Items.Add(endOfTheYearAssessmentItem);
								}
							}
							else if(CurrentUserRole == "unitadministrators")
							{
								if(kpi.StartTime <= DateTime.Now && kpi.FinishTime >= DateTime.Now)
								{
									if(CanProgressModificationRequest(kpi))
									{
										var progressModificationRequestItem = new System.Web.UI.WebControls.ListItem("申请修改进度");
										result.Items.Add(progressModificationRequestItem);
									}

									var progressReportItem = new System.Web.UI.WebControls.ListItem("进度上报");
									result.Items.Add(progressReportItem);
								}
							}
						}
						break;
					case DataAccess.Models.KPIStates.Year_End_Assessment_In_Progress:
						{
							if(CurrentUserRole == "administrators")
							{
								var endOfTheYearAssessmentItem = new System.Web.UI.WebControls.ListItem("年底考核");

								result.Items.Add(endOfTheYearAssessmentItem);
							}
						}
						break;
				}
			}

			return result;
		}

		private bool CanProgressModificationRequest(DataAccess.Models.KPI kpi)
		{
			var result = false;

			if(kpi != null)
			{
				var season = CommonHelper.GetSeason();

				if(season == "一季度")
				{
					result = kpi.TheKPIProgress != null && !string.IsNullOrWhiteSpace(kpi.TheKPIProgress.Quarter1Progress);
				}

				if(season == "二季度")
				{
					result = kpi.TheKPIProgress != null && !string.IsNullOrWhiteSpace(kpi.TheKPIProgress.Quarter2Progress);
				}

				if(season == "三季度")
				{
					result = kpi.TheKPIProgress != null && !string.IsNullOrWhiteSpace(kpi.TheKPIProgress.Quarter3Progress);
				}

				if(season == "四季度")
				{
					result = kpi.TheKPIProgress != null && !string.IsNullOrWhiteSpace(kpi.TheKPIProgress.Quarter4Progress);
				}
			}

			return result;
		}

		private void BindKPIProgress()
		{
			if(TheGroup != null)
			{
				using(var kpiProgressRepository = new DataAccess.Repository.KPIProgressRepository())
				{
					var lstKPIProgress = kpiProgressRepository.GetKPIProgressByGroup(TheGroup.GroupID, TheYear);

					if(lstKPIProgress != null && lstKPIProgress.Count > 0)
					{
						_ItemCount = lstKPIProgress.Count;

						rptKPIProgress.DataSource = lstKPIProgress;
						rptKPIProgress.DataBind();
					}
					else
					{
						kpisprogress.Visible = false;
					}
				}
			}
		}

		private bool ShowEndOfTheYearAssessment()
		{
			var result = false;

			if(CurrentUserRole.Equals("administrators") || CurrentUserRole.Equals("supervisitors") || (CurrentUserRole.Equals("unitadministrators") && CurrentUserGroupID == TheGroup.GroupID))
			{
				result = true;
			}

			return result;
		}
	}
}
